<?php

/**
 * This is the model class for table "cca_record".
 *
 * The followings are the available columns in table 'cca_record':
 * @property integer $matric_no
 * @property integer $cca_id
 * @property integer $role_level
 *
 * The followings are the available model relations:
 * @property Cca $cca
 * @property Resident $resident
 * @property Role $role
 */
class CcaResident extends CActiveRecord
{
	public $cca_name;
	public $resident_name;
	public $role_name;
	public $resident_AY;
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CcaResident the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'cca_record';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('matric_no, cca_id, role_level', 'required'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, matric_no, cca_id, role_level, point, cca_name, resident_name, role_name', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'cca' => array(self::BELONGS_TO, 'Cca', 'cca_id'),
			'resident' => array(self::BELONGS_TO, 'Resident', 'matric_no'),
			'role' => array(self::BELONGS_TO, 'Role', 'role_level'),
                        'point_record' => array(self::HAS_MANY, 'PointRecord', 'matric_no')
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
/*	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'matric_no' => 'Resident',
			'cca_id' => 'Cca',
			'role_level' => 'Role',
			'point' => 'Point',
			'cca_name' => 'CCA Name',
			'resident_name' => 'Name',
			//'resident_AY' => 'AY';
			'role_name' => 'Role',
		);
	}
*/
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

//		$criteria->compare('id',$this->id);
		$criteria->compare('matric_no',$this->matric_no);
		$criteria->compare('cca_id',$this->cca_id);
		$criteria->compare('role_level',$this->role_level);
//		$criteria->compare('point',$this->point);
		$criteria->together  =  true;
		$criteria->with = array('cca','resident','role');
		if($this->cca_name)
        {
                $criteria->compare('cca.name',$this->cca_name,true);
        }
        if($this->resident_name)
        {
                $criteria->compare('resident.name',$this->resident_name,true);
        }
        if($this->role_name)
        {
                $criteria->compare('role.name',$this->role_name,true);
        }
            if($this->resident_AY)
        {
                $criteria->compare('resident.AY',$this->resident_AY,true);
        }
		
		$sort=new CSort;
		$sort->attributes=array(
		  // For each relational attribute, create a 'virtual attribute' using the public variable name
		  'cca_name' => array(
			'asc' => 'cca.name',
			'desc' => 'cca.name DESC',
			'label' => 'CCA Name',
		  ),
		  'resident_name' => array(
			'asc' => 'resident.name',
			'desc' => 'resident.name DESC',
			'label' => 'Name',
		  ),
		  'role_name' => array(
			'asc' => 'role.name',
			'desc' => 'role.name DESC',
			'label' => 'Role',
		  ),
                 'resident_AY' => array(
			'asc' => 'resident.AY',
			'desc' => 'resident.AY DESC',
			'label' => 'AY',
		  ),
		  '*',
		);
    
		
		
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'sort'=>$sort,
		));
	}

}